<?php
namespace app\api\controller;
use think\Db;
/**
 * Description of Article
 * @author 高磊<1455209597@qq.com>
 */
class Article extends Api{
    //put your code here
   /**
    * 获取文章
    */
    public function getArticleList(){
        $data= $this->askData;
        if(!$this->val('getArticleList')){
            return $this->result= msback(150,'参数错误','');
        }
        $uid='';
        $host= $this->request->domain();
        if(isset($data['token'])){
           $uid= Db::name('user')->where(['token'=>$data['token']])->value('id');
        }
        $where=[
            'a.is_delete'=>0,
            'a.status'=>1
        ];
        if(!empty($data['cat_id'])){
           $where['a.category_id']=$data['cat_id'];
        }
        if(!empty($data['is_video_artcile'])){
            if($data['is_video_artcile']==1){
                $where['a.video_list']=['like',"%http%"];
            }           
        }
        $res= Db::name('article a')
                ->join('xy_user u','u.id = a.user_id','left')
                ->join('xy_article_like l','l.article_id = a.id','left')
                ->join('xy_copy_history h','h.article_id = a.id','left') 
                ->join('xy_article_catch ca','ca.article_id = a.id','left')
                ->join('xy_picture p','p.id = u.headimg','left')
                ->join('xy_category c','c.id = a.category_id','left')
                ->field('concat("'.$host.'",p.url) as headimgurl,count(l.id) as likenum,count(h.id) as copynum,a.video_list,a.other_share_num,a.id,a.view_num,u.nickname,a.is_hot,a.title,a.category_id as cat_id,a.pic_list,a.create_time,a.update_time,c.name,a.share_from_user_id,a.share_from_article_id')
                ->where($where)
                ->group('a.id')                                                                                                                                                                             
                ->order('a.other_share_num,a.create_time desc,a.view_num desc')
                ->limit($data['offset'],$data['page'])->select();
       
        foreach($res as $k=>$v){
              #文章列表
              if($v['pic_list']){
                  $host= $this->request->domain();
                  $plist= Db::name('picture')->where("id in (".$v['pic_list'].")")->field("concat('{$host}',url) as url")->select();
                  $res[$k]['pic_list']=$plist;
              }             
              #看下是否登录过
              $res[$k]['is_like']=0;
              $res[$k]['is_catch']=0;
              if($uid){
                  $is_res= Db::name('article_like')->where(['article_id'=>$v['id'],'user_id'=>$uid])->value('id');
                  if($is_res){
                      $res[$k]['is_like']=1;
                  }
                  $is_catch= Db::name('article_catch')->where(['article_id'=>$v['id'],'user_id'=>$uid])->value('id');
                  if($is_catch){
                      $res[$k]['is_catch']=1;
                  }
              }              
        }
   //     dump(db()->getLastSql());die;
        return $this->result=$res?msback(200,'请求成功',$res):msback(100,'无数据');
        
    }
   /**
    * 获取文章详情 
    */
    public function getArticleInfo(){
        $data= $this->askData;
        if(!$this->val('getArticleInfo')){
            return $this->result= msback(150,'参数错误','');
        }
        $uid='';
        if(isset($data['token'])){
           $uid= Db::name('user')->where(['token'=>$data['token']])->value('id');
        }    
        $host= $this->request->domain();
        $res= Db::name('article a')
                ->join('xy_user u','u.id = a.user_id','left')
                ->join('xy_article_like l','l.article_id = a.id','left')
                ->join('xy_picture p','p.id = u.headimg','left')
                ->join('xy_article_catch ca','ca.article_id = a.id','left')
                ->join('xy_category c','c.id = a.category_id')
                ->field("u.nickname,concat('{$host}',p.url) as headimg,a.is_hot,count(l.id) as likenum,a.other_share_num,a.category_id as cat_id,a.title,a.view_num,a.video_list,a.content,if(ca.id,1,0) as is_catch,a.id as article_id,a.pic_list,a.create_time,a.update_time,c.name as catname,a.share_from_user_id,a.share_from_article_id")
                ->where(['a.id'=>$data['article_id']])
                ->group('a.id')
                ->find();
        if(!$res){
            return $this->result= msback(151,'数据不存在','');
        }else{
            if(!empty($res['pic_list'])){
                $plist= Db::name('picture')->where("id in (".$res['pic_list'].")")->field("concat('{$host}',url) as url")->select();
                $res['pic_list']=$plist;
            }
            #更新浏览数
            Db::name('article')->where(['id'=>$data['article_id']])->setInc('view_num');
            #用户浏览记录记录
            $res['is_like']=0;
            $res['is_catch']=0;           
            if($uid){
                Db::name('view_history')->insert(['user_id'=>$uid,'article_id'=>$data['article_id'],'create_time'=> time()]);
                $is_like= Db::name('article_like')->where(['user_id'=>$uid,'article_id'=>$res['article_id']])->value('id');
                if($is_like){
                    $res['is_like']=1;
                }               
                $is_catch= Db::name('article_catch')->where(['article_id'=>$data['article_id'],'user_id'=>$uid])->value('id');
                if($is_catch){
                      $res['is_catch']=1;
                }
            }            
            return $this->result= msback(200,'获取成功',$res);         
        }
    }
    /**
     * 获取评论接口
     */
    public function getArticleComment(){
        $data= $this->askData;
        $host= $this->request->domain();
        if(!$this->val('getArticleComment')){
            return $this->result= msback(150,'参数错误','');
        }    
        $res= Db::name('comment c')
                ->join('xy_article a','a.id = c.article_id','left')
                ->join('xy_user u','u.id = c.user_id','left')
                ->join('xy_picture p','p.id = u.headimg','left')               
                ->field("concat('{$host}',p.url) as headimg,c.content,c.create_time,u.nickname")
                ->where(['a.id'=>$data['article_id']])
                ->limit($data['offset'],$data['page'])
                        ->order('a.create_time')
                ->select();
       return $this->result=$res?msback(200,'请求成功',$res):msback(100,'无数据');
    }
    /**
    * 搜索文章
    */
    public function searchArticle(){
        $data= $this->askData;
        if(!$this->val('searchArticle')){
            return $this->result= msback(150,'参数错误','');
        }
        if (!empty($data['token'])) {
            $uid = Db::name('user')->where(['token' => $data['token']])->value('id');
            if ($uid) {
                $is_exi = Db::name('search_history')->where(['uid' => $uid, 'key' => $data['key']])->value('id');
                if (empty($is_exi)) {
                    Db::name('search_history')->insert(['uid' => $uid, 'key' => $data['key'], 'create_time' => time()]);
                } else {
                    Db::name('search_history')->where(['id' => $is_exi])->update(['create_time' => time()]);
                }
            }
        }
        $res= Db::name('article a')
                ->join('xy_user u','u.id = a.user_id')
                ->join('xy_category c','c.id = a.category_id')
                ->whereOr(['a.title'=>['like','%'.$data['key'].'%']])
                ->whereOr(['a.content'=>['like','%'.$data['key'].'%']])
                ->whereOr(['a.keywords'=>['like','%'.$data['key'].'%']])
                ->field('a.title,a.create_time,u.nickname,c.name,a.view_num,a.id as article_id,a.category_id as cat_id')
                ->order('a.create_time desc,a.view_num')
                ->limit(10)
                ->select();
        return  $this->result=$res?msback(200,'有数据',$res):msback(100,'无数据');      
    }
    /**
     * 获取相关文章
     */
    public function getAboutArticle(){
        $data= $this->askData;
        if(!$this->val('getAboutArticle')){
            return $this->result= msback(150,'参数错误','');
        }   
        if(isset($this->askData['article_id'])){
            $aid=$this->askData['article_id'];
        }else{
            $aid=0;
        }
        $res= Db::name('article')
                ->where(['category_id'=>$data['cat_id']])
                ->order('create_time desc')
                ->field('title,id as article_id,create_time,category_id as cat_id,video_list')
                ->limit(5)
                ->where(['is_delete'=>0,'status'=>1,'id'=>['<>',$aid]])
                ->order('create_time desc,view_num desc')
                ->select();
        return $this->result=$res?msback(200,'请求成功',$res):msback(100,'无数据');
    }
    
}
